/** \file advanced_demo.ino
 * Demo program for the ezlcd3xx Arduino library.
 */

/*
 * Written and maintained by Sergey Butylkov. 
 * Created on behalf of EarthLCD.com a dba of Earth Computer Technologies Inc.
 *
 * Project home: http://arduino-ezlcd3xx.googlecode.com
 *
 * Copyright (C) 2012 Sergey Butylkov
 * Copyright (C) 2012 EarthLCD.com a dba of Earth Computer Technologies, Inc.
 * 
 * This work is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version. This work is distributed in the hope that it will be 
 * useful, but without any warranty; without even the implied warranty of 
 * merchantability or fitness for a particular purpose. See the GNU Lesser 
 * General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#include "ezlcd3xx.h"
#include "squaresdemo.h"

#define LED_PIN 13

#define ANALOG_PIN A0

//EzLCD3xx_HW lcd; // uses pins 0 and 1
//EzLCD3xx_SW lcd( 10, 11 ); // arLCD users must use this
EzLCD3xx_SW lcd( 6, 7 );

bool meters = false;
bool squares = false;
bool buttons = false;

void setup()
{
    lcd.begin( 9600 );
    lcd.fontw( 0, "sans24" );
    lcd.theme( 1, 155, 152, 3, 3, 1, 0, 1, 0, 0, 0 );
    //lcd.theme( 1, 1, 2, 0, 0, 0, 3, 1, 0, 3, 0 );
    lcd.cls( 0 );
    if( !meters && !squares && !buttons )
    {
        int choice = lcd.choice( "Display meters?", 1 );
        if( choice == YES )
            meters = true;
        else if( choice == NO )
        {
            choice = lcd.choice( "Display squares demo?", 1 );
            if( choice == YES )
                squares = true;
            else if( choice == NO )
            {
                choice = lcd.choice( "Button press demo?", 1 );
                if( choice == YES )
                    buttons = true;
            }
        }
    }
    if( meters )
    {
        pinMode( A0, INPUT );
        lcd.string( 1, "AMETER" );
        lcd.ameter( 1, 0, 1, 10, 40, 190, 190, 512, 0, 1024 );
        lcd.progress( 2, 1, 10, 10, 380, 20, 0, 1024 );
        lcd.dmeter( 3, 1, 210, 40, 100, 40, 0, 4, 0, true, CENTER );
    }
    if( squares )
    {
        demo_setup( lcd );
    }
    if( buttons )
    {
        pinMode( LED_PIN, OUTPUT );
        digitalWrite( LED_PIN, LOW );
        lcd.string( 1, "ON" );
        lcd.string( 2, "OFF" );
        lcd.button( 1, 1, 1, 10, 40, 185, 140 );
        lcd.button( 2, 1, 2, 205, 40, 185, 140 );
    }
}

int value = 0;

void loop()
{
#if EZLCD_SERIALDEBUG
    lcd.forwardSerial();
#endif
    if( meters )
    {
        int newvalue = analogRead( ANALOG_PIN );
        if( value != newvalue )
        {
            value = newvalue;
            lcd.ameter_value( 1, newvalue );
            lcd.progress_value( 2, newvalue );
            lcd.dmeter_value( 3, newvalue );
        }
    }
    if( squares )
        demo_loop( lcd );
    if( buttons )
    {
        if( lcd.pollLCD(1000L) )
        {
            if( lcd.wasReleased(1) )
                digitalWrite( LED_PIN, HIGH );
            else if( lcd.wasReleased(2) )
                digitalWrite( LED_PIN, LOW );
        }
    }
}
